606. 根据二叉树创建字符串
为保证权益,题目请参考 606. 根据二叉树创建字符串(From LeetCode).
解决方案1
Python
python
# 606. 根据二叉树创建字符串
# https://leetcode-cn.com/problems/construct-string-from-binary-tree/
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def tree2str(self, root: Optional[TreeNode]) -> str:
if root is None:
return ""
else:
ans = str(root.val)
if root.left is None:
if root.right is None:
pass
else:
ans += "()({})".format(self.tree2str(root.right))
else:
if root.right is None:
ans += "({})".format(self.tree2str(root.left))
else:
ans += "({0})({1})".format(self.tree2str(root.left), self.tree2str(root.right))
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30